/**
 * Created with CLion.
 * Description: 
 * User: loong
 * Date: 2019-12-30
 * Time: 14:46
 */
#include "MainForm.h"
#include "Ui_MainForm.h"


MainForm::MainForm(QWidget *parent) :
        QWidget(parent), ui(new Ui::MainForm) {
    ui->setupUi(this);
    timer = new QTimer(this);
    plantsDao = new PlantsDao();



    //1.初始化
    //1.1设备初始化
    device_init("hw:1,0");
    //1.2语音播放参数初始化,登录讯飞
    xfyun_init("56ee43d0");
    //1.3设置声音播放参数初始化
    set_voice_params("xiaoyan", 50, 50, 50, 0);
    //ui->tabWidget->removeTab(1);
    //ui->tabWidget->tabBar()->hide();

    ui->label->setText("无输入消息");
    //退出登录
    connect(ui->btn_sign_out, SIGNAL(clicked()), this, SLOT(signOut()));
    //显示时间
    connect(timer, SIGNAL(timeout()), this, SLOT(showTime()));
    timer->start(1000);
    //语音输入按下
    connect(ui->btn_input, SIGNAL(pressed()), this, SLOT(startCapture()));
    //播报
    connect(ui->tableView_query_show, SIGNAL(clicked(QModelIndex)), this, SLOT(startPlay(QModelIndex)));
    //显示二维码
    connect(ui->btn_qr,SIGNAL(clicked()),this,SLOT(ShowQrCode()));
    //生成二维码
    //connect(this, SIGNAL(toShowQrCode(string)), this, SLOT(genCode(QString)));
    //立即播放
    //connect(this, SIGNAL(playNow(QString)), this, SLOT(PLAY(QString)));
    //显示数据
    //connect(this, SIGNAL(toShowData(QString)), this, SLOT(showData(QString)));
}

MainForm::~MainForm() {
    delete ui;
}

void MainForm::setStyle(const QString &styleName) {
    QFile file(QString("%1").arg(styleName));
    file.open(QFile::ReadOnly);
    QString css = QLatin1String(file.readAll());
    qApp->setStyleSheet(css);

}

void MainForm::signOut() {
    emit backInitWimdow();
}

void MainForm::showTime() {
    QDateTime time = QDateTime::currentDateTime();
    QString str = time.toString("yyyy-MM-dd hh:mm:ss dddd");
    ui->label_time->setText(str);
}

int MainForm::startCapture() {
    ui->label->clear();
    //识别后的字符串
    QString string = speech_recognition();
    if (string != NULL) {
        string.replace("。", "");
        //移除字符串两端空白
        string.trimmed();
        ui->label->setText(string);
        qDebug() << "string-----" << string;
        //检查是否为数据库数据
        if (this->checkData(string)){
            //生成二维码
            //emit toShowQrCode(string);
            showCode = new ShowCode(this,plantsDao->getQrPath(string));
            showCode->hide();
            //QString s = plantsDao->getQrPath(string);
            //
            //showCode->GenerateQrCode(string);

            //显示数据
            dbHelper = new DBHelper();
            dbHelper->conn();
            queryModel = new QSqlQueryModel(ui->tableView_query_show);
            QString qString = QString("select id,name,base,local,purpose from plants where name='%1';").arg(string);
            queryModel->setQuery(qString);
            queryModel->setHeaderData(0, Qt::Horizontal, "ID");
            queryModel->setHeaderData(1, Qt::Horizontal, "名称");
            queryModel->setHeaderData(2, Qt::Horizontal, "基本信息");
            queryModel->setHeaderData(3, Qt::Horizontal, "产地");
            queryModel->setHeaderData(4, Qt::Horizontal, "用途");
            ui->tableView_query_show->setModel(queryModel);
            ui->tableView_query_show->verticalHeader()->hide();


            ////播放
            //QString temp = plantsDao->getAll(string);
            //qDebug() << temp;
            //QByteArray tempData = temp.toLocal8Bit();
            //char *value = (char *) tempData.data();
            //text_to_speech(value);
        } else{
            text_to_speech("该植物不存在！");
        }
        //emit playNow(string);
    } else {
        ui->label->setText("识别为空");
    }

}

//鼠标点击播放 槽函数
void MainForm::startPlay(QModelIndex index) {
    QString string = index.data().toString();
    qDebug() << string;
    QByteArray tempData = string.toLocal8Bit();
    char *value = (char *) tempData.data();
    text_to_speech(value);
}

//检测数据是否在数据库
bool MainForm::checkData(QString plantsName) {
    bool flag = false;
    if (plantsDao->check(plantsName)) {
        flag = true;
    } else {
        flag = false;
    }
    return flag;
}

void MainForm::PLAY(QString plantsName) {
    //检查是否为数据库已有的植物
    if (this->checkData(plantsName)) {
        //播放
        QString temp = plantsDao->getAll(plantsName);
        qDebug() << temp;
        QByteArray tempData = temp.toLocal8Bit();
        char *value = (char *) tempData.data();
        text_to_speech(value);
    } else {
        text_to_speech("该植物不存在！");
    }
}

void MainForm::showData(QString plantsName) {
    //显示数据
    dbHelper = new DBHelper();
    dbHelper->conn();
    queryModel = new QSqlQueryModel();
    QString qString = QString("select id,name,base,local,purpose from plants where name='%1';").arg(plantsName);
    queryModel->setQuery(qString);
    queryModel->setHeaderData(0, Qt::Horizontal, "ID");
    queryModel->setHeaderData(1, Qt::Horizontal, "名称");
    queryModel->setHeaderData(2, Qt::Horizontal, "基本信息");
    queryModel->setHeaderData(3, Qt::Horizontal, "产地");
    queryModel->setHeaderData(4, Qt::Horizontal, "用途");
    ui->tableView_query_show->setModel(queryModel);
}

void MainForm::genCode(QString plantsName) {
    ///生成二维码
    //emit toShowQrCode();

}

void MainForm::ShowQrCode() {
    showCode->show();
}






